<?php

/**
 * product actions.
 *
 * @package    SXShopCartPlugin
 * @subpackage product
 * @author     SoftXpert
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class shopCartAdminProductsActions extends SXActions
{

    /**
     * List all prodcuts in the system
     * @param sfWebRequest $request
     */
    public function executeIndex(sfWebRequest $request)
    {
        $page        = $request->getParameter('page', 1);
        $query       = ProductTable::getInstance()->getAllProducts();
        $this->pager = $this->createPager('Product', $query, $page, sfConfig::get('app_list_table_record_number'));
    }

    /*
     * Create new product form
     */

    public function executeNew(sfWebRequest $request)
    {
        $this->form = new ProductForm();
    }

    /**
     * save new product
     * @param sfWebRequest $request
     */
    public function executeCreate(sfWebRequest $request)
    {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new ProductForm();

        if ($this->processForm($request, $this->form))
        {
            $this->getUser()->setFlash('message_success', 'add new product success');
            $this->redirect('shopCartAdminProducts/index');
        }
        $this->setTemplate('new');
    }

    /**
     * edit product form 
     * @param sfWebRequest $request
     */
    public function executeEdit(sfWebRequest $request)
    {
        $this->forward404Unless($product = Doctrine_Core::getTable('product')->find(array($request->getParameter('id'))), sprintf('Object product does not exist (%s).', $request->getParameter('id')));
        $this->form = new ProductForm($product);
    }

    /**
     * Save edited product information
     * @param sfWebRequest $request
     */
    public function executeUpdate(sfWebRequest $request)
    {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($product = Doctrine_Core::getTable('product')->find(array($request->getParameter('id'))), sprintf('Object product does not exist (%s).', $request->getParameter('id')));
        $this->form = new ProductForm($product);

        // proces form function if the form valid return the object else return false
        if ($this->processForm($request, $this->form))
        {
            $this->getUser()->setFlash('message_success', 'update product success');
        }
        else
        {
            $this->getUser()->setFlash('message_failed', 'update product failed');
        }
        $this->setTemplate('edit');
    }

    /**
     * Delete product -soft delete- for keeping history
     * @param sfWebRequest $request
     */
    public function executeDelete(sfWebRequest $request)
    {
        $this->forward404Unless($product = Doctrine_Core::getTable('product')->find(array($request->getParameter('id'))), sprintf('Object product does not exist (%s).', $request->getParameter('id')));
        $product->delete();
        $this->getUser()->setFlash('message_success', 'delete product success');
        $this->redirect('shopCartAdminProducts/index');
    }

    /**
     * View product information
     * @param sfWebRequest $request
     */
    public function executeShow(sfWebRequest $request)
    {
        $this->forward404Unless($product = Doctrine_Core::getTable('product')->find(array($request->getParameter('id'))), sprintf('Object product does not exist (%s).', $request->getParameter('id')));
        $this->product = $product;
    }

    /**
     * Process and save forms
     * @param sfWebRequest $request
     * @param sfForm $form
     * @return mixed true or Product object
     */
    protected function processForm(sfWebRequest $request, sfForm $form)
    {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        $product = null;
        if ($form->isValid())
        {
            $product = $form->save();
        }
        return $product;
    }

}
